package com.vj.analyzeLog;

import java.io.IOException;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Assert;
import org.junit.Test;

//refer : http://www.solrtutorial.com/solrj-tutorial.html
public class SolrjPopulator {

	HttpSolrServer server = new HttpSolrServer("");

	@Test
	public void populate() throws SolrServerException, IOException{
		for(int i=0;i<1000;++i){
			SolrInputDocument doc = new SolrInputDocument();
			doc.addField("cat", "book");
			doc.addField("id", "book-"+i);
			doc.addField("name", "The Legend of the Hobbit part "+i);
			server.add(doc);
			if(i%100==0)
				server.commit(); // periodically flush
		}
		server.commit();
		
		Assert.assertTrue(false);
	}
	
	@Test
	public void query() throws SolrServerException {
		SolrQuery query = new SolrQuery();
		query.setQuery("sony digital camera");
		query.addFilterQuery("cat:electronics", "store:amazon.com");//what for
		query.setFields("id","price","merchant","cat","store");//what for
		query.setStart(0);
		query.set("defType", "edismax");//what for
		
		QueryResponse rsp = server.query(query);
		SolrDocumentList docs = rsp.getResults();
		for(int i=0;i<docs.size();++i){
			System.out.println(docs.get(i));
		}
		
		Assert.assertTrue(false);
	}
	
}
